package com.shop.shoporder.service.impl;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.shop.shoporder.common.ExcelUtil;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.UploadFileConfig;
import com.shop.shoporder.entity.order.*;
import com.shop.shoporder.mapper.order.*;
import com.shop.shoporder.service.ExportService;
import com.sun.tools.internal.ws.wsdl.document.soap.SOAPUse;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service("exportService")
public class ExportServiceImpl implements ExportService {
    @Autowired
    OrderExportMapper orderExportMapper;
    @Autowired
    DemolitionOrderMapper demolitionOrderMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ConfigMapper configMapper;
    @Autowired
    ExportRecordMapper exportRecordMapper;
    @Autowired
    UploadFileConfig uploadFileConfig;
    @Autowired
    CartMapper cartMapper;
    private static final int  BUFFER_SIZE = 2 * 1024;

    @Override
    public List<RemoveUserAndAddress> removeDuplicate(List<RemoveUserAndAddress> list) {
        List<RemoveUserAndAddress> distinctClass = list.stream().
                collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
                                        o -> o.getUserId() + ";" +
                                        o.getPhone()+ ";" +
                                        o.getLogname()+ ";" +
                                        o.getAddressAll()))), ArrayList::new));//+ ";" + o.getAddressId()
        return distinctClass;
    }

    @Override
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public OrderExport orderGroup(OrderExport orderExport) {
        OrderExport orderExportTemp = new OrderExport();
        orderExportTemp.setLogName(orderExport.getLogName());
        orderExportTemp.setPhone(orderExport.getPhone());
        orderExportTemp.setGuigeName(orderExport.getGuigeName());
        //orderExportTemp.setGuigeName1(orderExport.getGuigeName1());
        orderExportTemp.setAddressAll(orderExport.getAddressAll());
        orderExportTemp.setProductCount(orderExport.getProductCount());
        //orderExportTemp.setProvincename(orderExport.getProvincename());
        //orderExportTemp.setCityname(orderExport.getCityname());
        //orderExportTemp.setAreaname(orderExport.getAreaname());
        //orderExportTemp.setDetailAddr(orderExport.getDetailAddr());
        orderExportTemp.setWeight(orderExport.getWeight());
        orderExportTemp.setOrderNo(orderExport.getOrderNo());
        orderExportTemp.setSupplier(orderExport.getSupplier());
        orderExportTemp.setCartId(orderExport.getCartId());
        return orderExportTemp;
    }

    @Override
    public List<OrderExport> assOrderData(List<OrderExport> list2,String name) {
        //Config userConfig = configMapper.selectByPrimaryKey(1);
        List<OrderExport> listTemp1 = new ArrayList<>();
        OrderExport orderExport = new OrderExport();
        String names = "";
        double weight = 0.0;
        Set<String> setCartId = new HashSet();
        Set<String> setOrderNo = new HashSet();
        Set<String> setGuiGeName = new HashSet();
        for (OrderExport orderExport1 :list2) {
            String orderNo=orderExport1.getOrderNo()==null?"":orderExport1.getOrderNo();
            String supName=orderExport1.getSupplier()==null?"":orderExport1.getSupplier();
            if(supName.equals(name)&&!"".equals(orderNo)){
//                System.out.println("supName:"+supName+" name:"+name);
             orderExport = orderExport1;
            //判断重量是否是大于10公斤
            int weightdouble = orderExport1.getWeight() == null ? 0 : orderExport1.getWeight();
            int productCount = orderExport1.getProductCount() == null ? 0 : orderExport1.getProductCount();
            String ame=orderExport1.getNameSort()==null?"未知":orderExport1.getNameSort();
            weight += weightdouble * productCount;
            //System.out.println("orderno:"+orderExport1.getOrderNo());
//            System.out.println("weight:"+weight);
//            System.out.println("orderExport1.getGuigeName():"+orderExport1.getGuigeName());
//            if (weight > userConfig.getWeight()) {
//                OrderExport orderExportTemp = orderGroup(orderExport1);
//                orderExportTemp.setGuigeName(names);
//                orderExportTemp.setWeight(weightdouble);
//                orderExportTemp.setOrderNo(orderExport1.getOrderNo());
//                listTemp1.add(orderExportTemp);
//                names = "";
//                weight = 0;
//                names += orderExport1.getGuigeName();
//                weight += weightdouble * productCount;
//            } else {
//                setOrderNo.add(orderExport1.getOrderNo());
//                setGuiGeName.add(orderExport1.getGuigeName());
//            }
                setCartId.add(orderExport1.getCartId());
            setOrderNo.add(orderExport1.getOrderNo());
            setGuiGeName.add(orderExport1.getGuigeName());
            }
        }
        String nos="";
        String guiges="";
        String carts="";
        Set<String> setOrderNoNew = new HashSet();
        for (String set:setOrderNo) {
            if(set!=null&&!"".equals(set)){
                String []str=set.split(",");
                if(str.length==1){
                    setOrderNoNew.add(set);
                }else {
                    for (String str1:str) {
                        setOrderNoNew.add(str1);
                    }
                }
            }
        }
        List<String> list_1 = new ArrayList<>(setOrderNoNew);
        for (int i = 0; i <list_1.size() ; i++) {
            if(i==(list_1.size()-1)){
                nos +=list_1.get(i);
            }else {
                nos +=list_1.get(i)+",";
            }
        }
        List<String> list_2 = new ArrayList<>(setGuiGeName);
        for (int i = 0; i <list_2.size() ; i++) {
            if(i==(list_2.size()-1)){
                guiges +=list_2.get(i);
            }else {
                guiges +=list_2.get(i)+"+";
            }
        }
        List<String> list_3 = new ArrayList<>(setCartId);
        for (int i = 0; i <list_3.size() ; i++) {
            if(i==(list_3.size()-1)){
                carts +=list_3.get(i);
            }else {
                carts +=list_3.get(i)+",";
            }
        }
        if(!"".equals(guiges)){
            orderExport.setGuigeName(guiges);
        }
        if(!"".equals(nos)){
            orderExport.setOrderNo(nos);
        }
        if(!"".equals(carts)){
            orderExport.setCartId(carts);
        }
        if(orderExport!=null&&orderExport.getCartId()!=null){
            listTemp1.add(orderExport);
        }
        return listTemp1;
    }

    @Override
    public List<OrderExport> assOrderData(List<OrderExport> list2) {
        //Config userConfig = configMapper.selectByPrimaryKey(1);
        List<OrderExport> listTemp1 = new ArrayList<>();
        OrderExport orderExport = new OrderExport();
        String names = "";
        double weight = 0.0;
        Set<String> setCartId = new HashSet();
        Set<String> setOrderNo = new HashSet();
        Set<String> setGuiGeName = new HashSet();
        for (OrderExport orderExport1 :list2) {
            //System.out.println("orderExport1:"+orderExport1.toString());
                orderExport = orderExport1;
                //判断重量是否是大于10公斤
                int weightdouble = orderExport1.getWeight() == null ? 0 : orderExport1.getWeight();
                int productCount = orderExport1.getProductCount() == null ? 0 : orderExport1.getProductCount();
                String ame=orderExport1.getNameSort()==null?"未知":orderExport1.getNameSort();
                weight += weightdouble * productCount;
                //System.out.println("orderno:"+orderExport1.getOrderNo());
//            System.out.println("weight:"+weight);
//            System.out.println("orderExport1.getGuigeName():"+orderExport1.getGuigeName());
//            if (weight > userConfig.getWeight()) {
//                OrderExport orderExportTemp = orderGroup(orderExport1);
//                orderExportTemp.setGuigeName(names);
//                orderExportTemp.setWeight(weightdouble);
//                orderExportTemp.setOrderNo(orderExport1.getOrderNo());
//                listTemp1.add(orderExportTemp);
//                names = "";
//                weight = 0;
//                names += orderExport1.getGuigeName();
//                weight += weightdouble * productCount;
//            } else {
//                setOrderNo.add(orderExport1.getOrderNo());
//                setGuiGeName.add(orderExport1.getGuigeName());
//            }
                setOrderNo.add(orderExport1.getOrderNo());
                setGuiGeName.add(orderExport1.getGuigeName());
                setCartId.add(orderExport1.getCartId());
        }
        String nos="";
        String guiges="";
        String carts="";
        Set<String> setOrderNoNew = new HashSet();
        for (String set:setOrderNo) {
            if(set!=null&&!"".equals(set)){
                String []str=set.split(",");
                if(str.length==1){
                    setOrderNoNew.add(set);
                }else {
                    for (String str1:str) {
                        setOrderNoNew.add(str1);
                    }
                }
            }
        }
        List<String> list_1 = new ArrayList<>(setOrderNoNew);
        for (int i = 0; i <list_1.size() ; i++) {
            if(i==(list_1.size()-1)){
                nos +=list_1.get(i);
            }else {
                nos +=list_1.get(i)+",";
            }
        }
        List<String> list_2 = new ArrayList<>(setGuiGeName);
        for (int i = 0; i <list_2.size() ; i++) {
            if(i==(list_2.size()-1)){
                guiges +=list_2.get(i);
            }else {
                guiges +=list_2.get(i)+"+";
            }
        }
        List<String> list_3 = new ArrayList<>(setCartId);
        for (int i = 0; i <list_3.size() ; i++) {
            if(i==(list_3.size()-1)){
                carts +=list_3.get(i);
            }else {
                carts +=list_3.get(i)+",";
            }
        }
        //System.out.println("guiges:"+guiges.toString()+" nos:"+nos.toString()+" carts:"+carts.toString());
        orderExport.setGuigeName(guiges);
        orderExport.setOrderNo(nos);
        orderExport.setCartId(carts);
        if(orderExport!=null&&orderExport.getCartId()!=null){
            listTemp1.add(orderExport);
        }
        return listTemp1;
    }

    @Override
    public void toZip(List<File> srcFiles, OutputStream out) {
        /**
         * 压缩成ZIP 方法2
         * @param srcFiles 需要压缩的文件列表
         * @param out           压缩文件输出流
         * @throws RuntimeException 压缩失败会抛出运行时异常
         */
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null ;
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[BUFFER_SIZE];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1){
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("压缩完成，耗时：" + (end - start) +" ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils",e);
        }finally{
            if(zos != null){
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Transactional
    @Override
    public String exportExcel(HttpServletRequest request, HttpServletResponse response,Integer aid) {
        int Intsupid=-2;
        if(request==null){
            Intsupid=-1;
        }else {
            String supid=request.getParameter("supid");
            Intsupid= InterfaceVer.VerNumber(supid);
        }

        int adminid=InterfaceVer.VerNumber(aid+"");
        List<OrderExport> orderExportList = orderExportMapper.getOrderExportData(Intsupid);//物流单数据
        List<RemoveUserAndAddress> addressList=new ArrayList<>();//存放去重的用户收货信息
        List<OrderExport> listSpecial=new ArrayList<>();//特殊产品
        List<OrderExport> listSpecialOther=new ArrayList<>();//特殊以外的其他产品数据
        List<OrderExport> listTempSupplier=new ArrayList<>();//供货商产品
        List<DemolitionOrder> deOrders=demolitionOrderMapper.selectAll();//需要进行拆单的基础数据
        Set<String> OrderNoHashSet=new HashSet<>();//订单编号
        //Set<RemoveUserAndAddress> reAddressHashSet=new HashSet<>();//存放去重的用户收货信息
        String[] title = {"业务单号","订单号","收件人姓名","收件人手机","收件人地址","品名","数量","备注"};
        String fileName = "物流导单"+System.currentTimeMillis()+".xls";
        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        String busidstr = format2.format(new Date());
        String sheetName = "荟生新零售"+busidstr;
        String supName="";
        Set<String> listSupplier=new HashSet<>();
        Set<String> nosList=new HashSet<>();
        Set<String> cartSList=new HashSet<>();
        String suname ="贵州荟生生物科技有限公司";
        int up=0;
//        try {
            for (OrderExport orderExport:orderExportList) {
                String supplier = orderExport.getSupplier() == null ? "" : orderExport.getSupplier();
                if ("".equals(supplier)) {
                    supName = "未知";
                } else {
                    supName = supplier;
                }
                listSupplier.add(supName);
                int productCount=orderExport.getProductCount();
                //获取所有用户收货信息
                String sortName = orderExport.getNameSort();//规格简称
                RemoveUserAndAddress re = new RemoveUserAndAddress();
                re.setLogname(orderExport.getLogName().trim());
                re.setPhone(orderExport.getPhone().trim());
                re.setAddressAll(orderExport.getAddressAll().trim());
                addressList.add(re);
                //获取所有用户收货信息结束

                //判断产品数量拆物流单
                int productid=orderExport.getProductId();
                int guigeid=orderExport.getGuigeId();
                boolean isChai1 = deOrders.stream().anyMatch(str -> (productid == str.getProductid() && guigeid == str.getGuigeid() && str.getType() == 1));
                boolean isChai2 = deOrders.stream().anyMatch(str -> (productid == str.getProductid() && guigeid == str.getGuigeid() && str.getType() == 2));
                if (!suname.equals(supName)) {
                    listTempSupplier.add(orderExport);
                }
                    else if(isChai1){
                        for (int n = 0; n < productCount; n++) {
                            OrderExport orderExportTemp = orderExport;
                            //添加购物车ID
                            cartSList.add(orderExport.getCartId()+"");
                            //
                            orderExportTemp.setGuigeName(sortName + "*" + 1);
                            listSpecial.add(orderExportTemp);
                        }
                    }else if(isChai2){
                        List<DemolitionOrder> list1q=deOrders.stream().filter(c -> (c.getProductid()==productid&&c.getGuigeid()==guigeid)).collect(Collectors.toList());
                        int chuCount=0;
                        if(list1q!=null&&list1q.size()>0){
                            chuCount=list1q.get(0).getNumber()==null?0:list1q.get(0).getNumber();
                        }
                        int c = productCount / chuCount;// a-(a/b)*b
                        if(c>0){
                            for (int i = 1; i <=c; i++) {
                                //添加购物车ID
                                cartSList.add(orderExport.getCartId()+"");
                                //
                                OrderExport orderExportTemp = orderGroup(orderExport);
                                orderExportTemp.setGuigeName(sortName + "*" + chuCount);
                                listSpecial.add(orderExportTemp);
                            }
                        }
                        if (productCount % chuCount != 0) {//处理剩下不到一件的
                            //添加购物车ID
                            cartSList.add(orderExport.getCartId()+"");
                            //
                            int d = productCount - (productCount / chuCount) * chuCount;
                            OrderExport orderExportTemp = orderGroup(orderExport);
                            orderExportTemp.setGuigeName(sortName + "*" + d);
                            listSpecial.add(orderExportTemp);
                        }
                    }else {
                        //添加购物车ID
                        cartSList.add(orderExport.getCartId()+"");
                        //
                        listSpecialOther.add(orderExport);
                    }
                //去除用户重复的收货信息
            }
            List<RemoveUserAndAddress> reAddressList = removeDuplicate(addressList);
            //根据去重的用户收货信息进行整合特殊以外产品信息

            for (RemoveUserAndAddress item : reAddressList) {
                List<OrderExport> list2 = listSpecialOther.stream().filter(c -> (
                                c.getLogName() != null && c.getLogName().equals(item.getLogname()) &&
                                c.getPhone() != null && c.getPhone().equals(item.getPhone()) &&
                                c.getAddressAll() != null && c.getAddressAll().equals(item.getAddressAll()))
                ).collect(Collectors.toList());
                Collections.sort(list2, Comparator.comparingInt(OrderExport::getProductCount));//根据数量排序
                List<OrderExport> listTemp1 = new ArrayList<>();
                if (list2 != null && list2.size() > 0) {
                    listTemp1 = assOrderData(list2);
                }
                if (listTemp1 != null && listTemp1.size() > 0) {
                    listSpecial.addAll(listTemp1);
                }
            }
    for (String str : listSupplier) {
        for (RemoveUserAndAddress item : reAddressList) {
            List<OrderExport> list2 = listTempSupplier.stream().filter(c -> (
                            c.getLogName() != null && c.getLogName().equals(item.getLogname()) &&
                            c.getPhone() != null && c.getPhone().equals(item.getPhone()) &&
                            c.getAddressAll() != null && c.getAddressAll().equals(item.getAddressAll()))
            ).collect(Collectors.toList());
            Collections.sort(list2, Comparator.comparingInt(OrderExport::getProductCount));//根据数量排序
            List<OrderExport> listTemp1 = new ArrayList<>();
            if (list2 != null && list2.size() > 0) {
                listTemp1 = assOrderData(list2,str);
            }
            if (listTemp1 != null && listTemp1.size() > 0) {
                listSpecial.addAll(listTemp1);
            }
        }
        }
            //组合数据添加到 Excel
            if(listSpecial!=null&&listSpecial.size()>0){
                Collections.sort(listSpecial, Comparator.comparing(OrderExport::getGuigeName));//排序
            }

            String [][] content=new String[listSpecial.size()][];
//
//        if(listTemp.size()<=0){
//            content=new String[1][];
//            content[0] = new String[title.length];
//            if(error.equals("")){
//                content[0][0] = "空空如也!";content[0][1] = "空空如也!";content[0][2] = "空空如也!";content[0][3] = "空空如也!";
//                content[0][4] = "空空如也!";content[0][5] = "空空如也!";content[0][6] = "空空如也!";
//            }else {
//                content[0][0] = error;
//            }
//        }
//        for (int i = 0; i < listTemp.size(); i++){
//            OrderExport orderExport=listTemp.get(i);
//            content[i] = new String[title.length];
//            content[i][0] = busidstr+"-"+(i+1);//业务单号
//            content[i][1] = orderExport.getLogname();
//            content[i][2] = orderExport.getPhone();
//            content[i][3] = orderExport.getAddressAll();
//            content[i][4] = orderExport.getGuigeName();
//            content[i][5] = "";
//            content[i][6] = orderExport.getSupplier();
//            content[i][7] =orderExport.getOrderNo();
//        }
            //创建HSSFWorkbook

            List<String> listSup1 = new ArrayList<>(listSupplier);
            List<File> wb = ExcelUtil.getHSSFWorkbookZip(sheetName, title, content, null,listSup1,listSpecial);
            String url="";
            String zipUrl="";
            try {
                String path="/usr/local/mall_uploads/logistics";
                //String path="D:/logzip";
                File uploadPath=new File(path);
                if(!uploadPath.exists()){
                    uploadPath.mkdirs();
                }
                zipUrl="HSYS"+System.currentTimeMillis()+".zip";
                url="/logistics/"+zipUrl;
                FileOutputStream fos2 = new FileOutputStream(uploadPath+"/"+zipUrl);
                toZip(wb,fos2);
                try {
                    try {
                        //上传到七牛云
                        File file = new File(uploadPath+"/"+zipUrl);
                        FileInputStream fileInputStream = new FileInputStream(file);
                        System.out.println("file----->" + file.getName());
                        MultipartFile multipartFile = new MockMultipartFile(zipUrl, file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
                        fileInputStream.close();
                        //构造一个带指定Zone对象的配置类
                        Configuration cfg = new Configuration(Zone.zone2());
                        //...其他参数参考类注释
                        UploadManager uploadManager = new UploadManager(cfg);
                        //...生成上传凭证，然后准备上传
                        //获取流
//            byte[] uploadBytes = multipartFile.getBytes();
                        byte[] uploadBytes = multipartFile.getBytes();
                        //默认不指定key的情况下，以文件内容的hash值作为文件名
                        String key = zipUrl;
                        Auth auth = Auth.create(uploadFileConfig.getAccessKey(), uploadFileConfig.getSecretKey());
                        String upToken = auth.uploadToken(uploadFileConfig.getExportName());
                        com.qiniu.http.Response responseLIU = uploadManager.put(uploadBytes, key, upToken);
                        String filePath = uploadFileConfig.getExportUrl() + "/" + zipUrl;
                        url=filePath;
                        // 上传到七牛云
                    }catch (QiniuException e){
                        System.out.println(e.error());
                        throw new RuntimeException();
                    }
                }catch (Exception es){
                    throw new RuntimeException();
                }


                //return new Response(MessageConstants.MSG_SUCCESS, filePath, 0, "", request);
                try {
                    setResponseHeader(response,zipUrl);
                    OutputStream os = response.getOutputStream();
                    FileInputStream fis = new FileInputStream(uploadPath+"/"+zipUrl);
                    int n = 0;
                    byte[] buff = new byte[1024];
                    while ((n = fis.read(buff)) != -1) {
                        os.write(buff, 0, n);
                    }
                    os.flush();
                    os.close();
                } catch (Exception ee) {
                    System.out.println("bb");
                    ee.printStackTrace();
                }

            } catch (FileNotFoundException e1) {
                System.out.println("aa");
                e1.printStackTrace();
            }
            if(up==0){
                Set<String> ss=new HashSet();
                for (OrderExport orderExport1:listSpecial) {
                    //System.out.println("orderExport1:"+orderExport1.getCartId()+" order:"+orderExport1.getOrderNo());
                   String s=orderExport1.getCartId();
                   if(s!=null&&!s.equals("")){
                       String []str=s.split(",");
                       for (String str1:str) {
                           if(str1!=null&&!str1.equals("")){
                               cartSList.add(str1);
                           }
                       }
                   }
                     //cartSList=dealOrderNo(orderExport1.getCartId()+"",cartSList);
                    nosList=dealOrderNo(orderExport1.getOrderNo(),nosList);
                }

                for (String str:cartSList) {
                    String []str1=str.split(",");
                    for (String str2:str1) {
                        if(str2!=null&&!str2.equals("")){
                            ss.add(str2);
                        }
                    }
                }

                List lists=null;
                List listsCart=null;
                try {
                    Object[] list1X=nosList.toArray();
                    String[] stringArray = Arrays.copyOf(list1X, list1X.length, String[].class);
                    lists = java.util.Arrays.asList(stringArray);
                }catch (Exception e12){}
                try {
                    Object[] list1X=ss.toArray();
                    String[] stringArray = Arrays.copyOf(list1X, list1X.length, String[].class);
                    listsCart = java.util.Arrays.asList(stringArray);
                }catch (Exception e12){}

                if(listsCart!=null&&listsCart.size()>0){
                    //HttpSession session=request.getSession();
                    //Integer adminid= (Integer) session.getAttribute("adminId");
                    String orderNos=nosList.toString().trim().replace('[','(').replace(']',')');
//                    int res= orderMapper.upOrderFrom(new Date(),listsCart);
                    //System.out.println("orderNos:"+orderNos);
                    //System.out.println("listsCart:"+listsCart.toString());
                    int res= cartMapper.upCartFrom(new Date(),listsCart);
                    if(res>0){
                        ExportRecord exportRecord=new ExportRecord();
                        exportRecord.setNumber(listSpecial.size());
                        exportRecord.setType(1);
                        exportRecord.setTypename("快递");
                        exportRecord.setUrl(url);
                        exportRecord.setTime(new Date());
                        exportRecord.setOrdernos(orderNos);
                        exportRecord.setSupplier(listSup1.toString());
                        exportRecord.setAdminuserid(adminid);
                        //exportRecord.setAdminname("adminname");
                        int res1=exportRecordMapper.insertSelective(exportRecord);
                        if(res1>0){

                        }else {
                            System.out.println("sss:");
                            throw new RuntimeException();
                        }
                    }else {
                        System.out.println("sss1:");
                        throw new RuntimeException();
                    }

                }
            }
            return "1";
//        }catch (Exception e){
//            System.out.println(e.getMessage());
//            throw new RuntimeException();
//        }
    }

    @Override
    public void exportExcel() {

    }

    @Override
    public Set dealOrderNo(String nos,Set<String> nosList) {
        Set<String> ha=new HashSet<>();
        String[] strings = nos.split(",");
        if (strings.length == 1) {
            ha.add(nos);
        } else {
            for (String str : strings) {
                ha.add(str);
            }
        }
        nosList.addAll(ha);
        return nosList;
    }

    @Override
    public void doExport(String path, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        File filex = new File(ResourceUtils.getURL("/usr/local/mall_uploads/").getPath());
        //File logPath = new File(filex.getAbsolutePath(), "/logistics");
        String aFilePath=filex+path;
        File xf=new File(aFilePath);
        String aFileName = xf.getName();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File file = null;
        file = new File(aFilePath);
        try{
            request.setCharacterEncoding("UTF-8");
            String agent = request.getHeader("User-Agent").toUpperCase();
            if ((agent.indexOf("MSIE") > 0) || ((agent.indexOf("RV") != -1) && (agent.indexOf("FIREFOX") == -1)))
                aFileName = URLEncoder.encode(aFileName, "UTF-8");
            else {
                aFileName = new String(aFileName.getBytes("UTF-8"), "ISO8859-1");
            }
            response.setContentType("application/x-msdownload;");
            response.setHeader("Content-disposition", "attachment; filename=" + aFileName);
            response.setHeader("Content-Length", String.valueOf(file.length()));
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
                bos.write(buff, 0, bytesRead);
            System.out.println("success");
            bos.flush();
        }catch (Exception e) {
            // TODO: handle exception
            System.out.println("导出文件失败！");
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
//                file.delete();
            } catch (Exception e) {
//               LOGGER.error("导出文件关闭流出错！", e);
            }
        }
    }

    @Transactional
    @Override
    public String readExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        try {
            List<String[]> list=ExcelUtil.readExcel(file);
            int count=0;
            for (int i = 0; i < list.size(); i++) {
                String orderNumber="";
                String logisicalNo="";
                int a3=0;
                List<String> orderList=new ArrayList<>();
                String[] strings = list.get(i);
                String a1=list.get(0)[0];
                String a2=list.get(0)[1];
                try {
                    a3=list.get(0)[2]==null?0:Integer.parseInt(list.get(0)[2]);
                }catch (Exception e){}
                if("订单号".equals(a1)&&"物流单号".equals(a2)){
                    String s1=strings[0]==null?"":strings[0];
                    String s2=strings[1]==null?"":strings[1];
                    if(!"".equals(s1)&&!"".equals(s2)){
                        if(i!=0){
                            String ss=strings[0].trim().
                                    replace("，",",").
                                    replace(",,",",").
                                    replace(" ",",").
                                    replace("  ",",").
                                    replace("   ",",");
                            String []orderStr=ss.split(",");
                            for (String str: orderStr ) {
                                orderNumber=str.trim();
                                orderList.add(orderNumber);
                            }
                            logisicalNo=strings[1].trim();
                        }
                    }else if("".equals(s1)&&"".equals(s2)) {
                        continue;
                    }else {
                        String s="";
                        if(orderList!=null&&orderList.size()>0){
                            s=orderList.get(0);
                        }
                        return "失败:第"+(i+1)+"行数据有误！"+s+"--"+logisicalNo;
                    }
                }else {
                    return "失败:标题1为订单号，标题2为物流单号！";
                }
                //hashMap.put(orderNumber,logisicalNo);
                //                    else if("姓名".equals(list.get(0)[j].toString()))
//                    {
//                        System.out.println("姓名:"+strings[j].toString());
//                    }
//                    else if("电话".equals(list.get(0)[j].toString()))
//                    {
//                        System.out.println("电话:"+strings[j].toString());
//                    }
//                    else if("地址".equals(list.get(0)[j].toString()))
//                    {
//                        System.out.println("地址:"+strings[j].toString());
//                    }
//                    else if("数量".equals(list.get(0)[j].toString()))
//                    {
//                        System.out.println("数量:"+strings[j].toString());
//                    }
//                    else if("商品".equals(list.get(0)[j].toString()))
//                    {
//                        System.out.println("商品:"+strings[j].toString());
//                    }
//                    else
//                    {
//                        //System.out.println(strings[j].toString());
//                    }
//                }
                if((logisicalNo != null && logisicalNo.length()!= 0)&&(orderNumber != null && orderNumber.length()!= 0)) {
                    int res=0;
                    for (String str:orderList) {
                        if(str!=null&&!str.equals("")) {

                            List<Cart> cartList = cartMapper.selectListByOrderNo(str);
                            if(cartList!=null&&cartList.size()>0){
                                Set<String> setorno = new HashSet();
                                for (Cart cart:cartList) {
                                    String logNo=cart.getLogisticalNo()==null?"":cart.getLogisticalNo();
                                    if(!"".equals(logNo)){
                                        for (String ss:logNo.split(",")) {
                                            if(ss!=null&&!ss.equals(""))setorno.add(ss);
                                        }
                                    }
                                }
                                setorno.add(logisicalNo);
                                String logNos="";
                                if(setorno!=null&&setorno.size()>0){
                                    List<String> list1 = new ArrayList<>(setorno);
                                    for (int f = 0; f <list1.size() ; f++) {
                                        String s=null;
                                        if(f==(list1.size()-1)){
                                            logNos +=list1.get(f);
                                        }else {
                                            logNos +=list1.get(f)+",";
                                        }
                                    }
                                }
                                List<String> list2Order=new ArrayList<>();
                                list2Order.add(str);
                                res = cartMapper.updateLogByOrderNo(logNos, list2Order);
                                if(res<=0){
                                    throw new RuntimeException();
                                }
                            }
                        }
                    }
//                    res = cartService.updateLogByOrderNo(logisicalNo, orderList);
                    Order order=orderMapper.selectByOrderNo(orderNumber);
                    if(order!=null) {
                        String orderStatus = order.getOrderStatus() == null ? "" : order.getOrderStatus();
                        if (!(orderStatus.equals("702") || orderStatus.equals("703"))) {
                            return "失败:订单状态不正确！" + orderNumber;
                        }
                    }else {
                        return "失败:订单号不正确！" + orderNumber;
                    }
                    int res1=orderMapper.updateOrderStatus1("703",new Date(),orderList);
                    if (res1<=0) {
                        return "更新失败:"+orderNumber;
                    }else {
                        count =count+1;
                    }
                }
            }
            return "总数量："+(list.size()-1)+" 成功数量："+count;
        }
        catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    // @Override
//    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
//        List<RemoveUserAndAddress> listOther=new ArrayList<>();//存放去重的用户收货信息
//        List<OrderExport> listEx1 = orderExportService.getOrderExportData1();//
//        List<OrderExport> list = listEx1;//导单数据
//        List<OrderExport> listTemp=new ArrayList<>();//特殊产品
//        List<OrderExport> listTempOther=new ArrayList<>();//特殊以外的其他产品数据
//        List<OrderExport> listTempSupplier=new ArrayList<>();//供货商产品数据
//        List<DemolitionOrder> demolitionOrders=demolitionOrderService.selectAll();//需要进行拆单的基础数据
//        HashSet<String> hashSet=new HashSet<>();//订单编号
//        int number=0;//导单数量
//        int up=0;
//        String[] title = {"业务单号","收件人姓名","收件人手机","收件人地址","品名","数量","备注","订单号"};
//        String fileName = "物流导单"+System.currentTimeMillis()+".xls";
//        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
//        String busidstr = format2.format(new Date());
//        String sheetName = "HSYS"+busidstr;
//        OrderExport errorOO=new OrderExport();
//        Set<String> listSupplier=new HashSet<>();
//        String supName="";
//        String suname="贵州荟生生物科技有限公司";
//        try {
//            for (int i = 0; i < list.size(); i++) {
//                OrderExport orderExport = list.get(i);
//                String supplier = orderExport.getSupplier() == null ? "" : orderExport.getSupplier();
//                if ("".equals(supplier)) {
//                    supName = "未知";
//                } else {
//                    supName = supplier;
//                }
//                if(!supName.equals(suname)){
//                    listSupplier.add(supName);
//                }
//                errorOO = list.get(i);
//                String[] strings = orderExport.getOrderNo().split(",");
//                if (strings.length == 1) {
//                    hashSet.add(orderExport.getOrderNo());
//                } else {
//                    for (String str : strings) {
//                        hashSet.add(str);
//                    }
//                }
//                //获取所有用户收货信息
//                RemoveUserAndAddress orderExport1 = new RemoveUserAndAddress();
////                orderExport1.setUserId(orderExport.getUserId());
//                orderExport1.setLogname(orderExport.getLogname());
//                orderExport1.setPhone(orderExport.getPhone());
//                orderExport1.setAddressAll(orderExport.getAddressAll());
//                String name = list.get(i).getGuigeName1();
//                //orderExport1.setAddressId(orderExport.getAddressId());
//                listOther.add(orderExport1);
//                //获取所有用户收货信息结束
//                int productId = 0, guigeId = 0, productCount = 0;
//                productId = list.get(i).getProductId();
//                guigeId = list.get(i).getGuigeId();
//                productCount = list.get(i).getProductCount();
//                //判断产品大于一定数量是否拆单
//                int finalProductId = productId;
//                int finalGuigeId = guigeId;
//                //判断产品大于一定数量是否拆单
//                boolean isChai = demolitionOrders.stream().anyMatch(str -> (finalProductId == str.getProductid() && finalGuigeId == str.getGuigeid() && str.getType() == 1));
//                boolean isChai1 = demolitionOrders.stream().anyMatch(str -> (finalProductId == str.getProductid() && finalGuigeId == str.getGuigeid() && str.getType() == 2));
//                if (!suname.equals(supName)) {
//                    listTempSupplier.add(orderExport);
//                } else if (isChai) {//类型为1拆单
//                    for (int n = 0; n < productCount; n++) {
//                        OrderExport orderExportTemp = orderExport;
//                        orderExportTemp.setGuigeName(name + "*" + 1);
//                        listTemp.add(orderExportTemp);
//                        number++;
//                    }
//                } else if (isChai1) {//类型为2拆单
//                    int c = productCount / 6;// a-(a/b)*b
//                    if (c == 1) {//单瓶整除为1
//                        OrderExport orderExportTemp = exportService.orderGroup(orderExport);
//                        orderExportTemp.setGuigeName(name + "*" + 6);
//                        listTemp.add(orderExportTemp);
//                        number++;
//                    }
//                    if (c > 1) {//单瓶整除大于1
//                        for (int n = 0; n < c; n++) {
//                            OrderExport orderExportTemp = exportService.orderGroup(orderExport);
//                            orderExportTemp.setGuigeName(name + "*" + 6);
//                            listTemp.add(orderExportTemp);
//                            number++;
//                        }
//                    }
//                    if (productCount % 6 != 0) {//处理剩下不到一件的
//                        int d = productCount - (productCount / 6) * 6;
//                        OrderExport orderExportTemp = exportService.orderGroup(orderExport);
//                        orderExportTemp.setGuigeName(name + "*" + d);
//                        listTemp.add(orderExportTemp);
//                        number++;
//                    }
//                } else {
//                    //添加到特殊产品拆单以外的其它集合
//                    listTempOther.add(orderExport);
//                }
//            }
//            //根据用户合并其它产品
//            //去除用户重复的收货信息
//            List<RemoveUserAndAddress> list1 = exportService.removeDuplicate(listOther);
//
//            //根据去重的用户收货信息进行整合特殊以外产品信息
//            List<OrderExport> listTempXX = new ArrayList<>();
//            for (String str : listSupplier) {
//                for (RemoveUserAndAddress item : list1) {
//                    List<OrderExport> list2 = listTempSupplier.stream().filter(c -> (
//                            c.getLogname() != null && c.getLogname().equals(item.getLogname()) &&
//                                    c.getPhone() != null && c.getPhone().equals(item.getPhone()) &&
//                                    c.getAddressAll() != null && c.getAddressAll().equals(item.getAddressAll()))
//                    ).collect(Collectors.toList());
//                    Collections.sort(list2, Comparator.comparingInt(OrderExport::getProductCount));//根据数量排序
//                    if (list2 != null && list2.size() > 0) {
//                        listTempXX = exportService.assOrderDataName(list2,str);
//                    }
//                    if (list2 != null && list2.size() > 0) {
//                        if(listTempXX!=null&&listTempXX.size()>0){
//                            List<OrderExport> list1q=listTempXX.stream().filter(c -> (!c.getOrderNo().equals("null")&&!c.getOrderNo().equals("")))
//                                    .collect(Collectors.toList());
//                            listTempXX=list1q;
//                            listTemp.addAll(listTempXX);
//                            number = number + listTempXX.size();
//                        }
//                    }
//                }
//            }
//            for (RemoveUserAndAddress item : list1) {
//                List<OrderExport> list2 = listTempOther.stream().filter(c -> (
////                                    c.getUserId()!=null&&c.getUserId().equals(item.getUserId())&&
//                                c.getLogname() != null && c.getLogname().equals(item.getLogname()) &&
//                                        c.getPhone() != null && c.getPhone().equals(item.getPhone()) &&
//                                        c.getAddressAll() != null && c.getAddressAll().equals(item.getAddressAll()))
//                ).collect(Collectors.toList());
//                Collections.sort(list2, Comparator.comparingInt(OrderExport::getProductCount));//根据数量排序
//                List<OrderExport> listTemp1 = new ArrayList<>();
//                if (list2 != null && list2.size() > 0) {
//                    listTemp1 = exportService.assOrderData(list2);
//                }
//
//                //再次进行组合
//                if (list2 != null && list2.size() > 0) {
//                    if(listTemp1!=null&&listTemp1.size()>0){
//                        listTemp.addAll(listTemp1);
//                        number = number + listTemp1.size();
//                    }
//                }
//            }
//
//        }catch(Exception e){
//            up = 1;
//        }
//        //组合数据添加到Excel
//        if(listTemp!=null&&listTemp.size()>0){
//            Collections.sort(listTemp, Comparator.comparing(OrderExport::getLogname));//排序
//        }
//
//        String [][] content=new String[listTemp.size()][];
////
////        if(listTemp.size()<=0){
////            content=new String[1][];
////            content[0] = new String[title.length];
////            if(error.equals("")){
////                content[0][0] = "空空如也!";content[0][1] = "空空如也!";content[0][2] = "空空如也!";content[0][3] = "空空如也!";
////                content[0][4] = "空空如也!";content[0][5] = "空空如也!";content[0][6] = "空空如也!";
////            }else {
////                content[0][0] = error;
////            }
////        }
////        for (int i = 0; i < listTemp.size(); i++){
////            OrderExport orderExport=listTemp.get(i);
////            content[i] = new String[title.length];
////            content[i][0] = busidstr+"-"+(i+1);//业务单号
////            content[i][1] = orderExport.getLogname();
////            content[i][2] = orderExport.getPhone();
////            content[i][3] = orderExport.getAddressAll();
////            content[i][4] = orderExport.getGuigeName();
////            content[i][5] = "";
////            content[i][6] = orderExport.getSupplier();
////            content[i][7] =orderExport.getOrderNo();
////        }
//        //创建HSSFWorkbook
//        listSupplier.add(suname);
//        List<String> listSup1 = new ArrayList<>(listSupplier);
//        listSup1.remove(suname);
//        List<String> ss=new ArrayList<>();
//        ss.add(suname);
//        ss.addAll(listSup1);
//        List<File> wb = ExcelUtil.getHSSFWorkbookZip(sheetName, title, content, null,ss,listTemp);
//        String url="";
//        String zipUrl="";
//        try {
//            String path="/usr/local/mall_uploads/logistics";
//            //String path="D:/logzip";
//            File uploadPath=new File(path);
//            if(!uploadPath.exists()){
//                uploadPath.mkdirs();
//            }
//            zipUrl="HSYS"+System.currentTimeMillis()+".zip";
//            url="/logistics/"+zipUrl;
//            FileOutputStream fos2 = new FileOutputStream(uploadPath+"/"+zipUrl);
//            toZip(wb,fos2);
//
//            try {
//                setResponseHeader(response,zipUrl);
//                OutputStream os = response.getOutputStream();
//                FileInputStream fis = new FileInputStream(uploadPath+"/"+zipUrl);
//                int n = 0;
//                byte[] buff = new byte[1024];
//                while ((n = fis.read(buff)) != -1) {
//                    os.write(buff, 0, n);
//                }
//                os.flush();
//                os.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
////        //
////        String url="";
////        //保存excel
////        try {
////            String path="/usr/local/mall_uploads/logistics";
////            File uploadPath=new File(path);
////            if(!uploadPath.exists()){
////                uploadPath.mkdirs();
////            }
////            FileOutputStream fileOut = new FileOutputStream(uploadPath+"/"+fileName);
////            url="/logistics/"+fileName;
////            wb.write(fileOut);
////            fileOut.close();
////        }
////        catch (Exception e){
////            e.printStackTrace();
////            up=1;
////        }
//        //响应到客户端
////        ZipOutputStream zos = null ;
////        try {
////            setResponseHeader(response,zipUrl);
////            OutputStream os = response.getOutputStream();
////            zos.write(os);
////            os.flush();
////            os.close();
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
//
//        if(up==0){
//            //更新为导单状态
//            List lists=null;
//            try {
//                Object[] list1X=hashSet.toArray();
//                String[] stringArray = Arrays.copyOf(list1X, list1X.length, String[].class);
//                lists = java.util.Arrays.asList(stringArray);
//            }catch (Exception e){}
//
//            if(lists!=null&&lists.size()>0){
//                HttpSession session=request.getSession();
//                Integer adminid= (Integer) session.getAttribute("adminId");
//                String orderNos=hashSet.toString().trim().replace('[','(').replace(']',')');
//
//                int res=orderService.upOrderFrom(lists,new Date());
//                ExportOrder exportOrder=new ExportOrder();
//                exportOrder.setNumber(number);
//                exportOrder.setType(1);
//                exportOrder.setTypename("快递");
//                exportOrder.setUrl(url);
//                exportOrder.setTime(new Date());
//                exportOrder.setOrdernos(orderNos);
//                exportOrder.setAdminuserid(adminid);
//                int res1=exportOrderService.insertSelective(exportOrder);
//            }
//        }
//    }

//    @Override
//    public void exportExcel() {
//        List<RemoveUserAndAddress> listOther=new ArrayList<>();//存放去重的用户收货信息
//        List<OrderExport> listEx1 = orderExportService.getOrderExportData1();//
//        List<OrderExport> list = listEx1;//导单数据
//        List<OrderExport> listTemp=new ArrayList<>();//特殊产品
//        List<OrderExport> listTempOther=new ArrayList<>();//特殊以外的其他产品数据
//        List<OrderExport> listTempSupplier=new ArrayList<>();//供货商产品数据
//        List<DemolitionOrder> demolitionOrders=demolitionOrderService.selectAll();//需要进行拆单的基础数据
//        HashSet<String> hashSet=new HashSet<>();//订单编号
//        int number=0;//导单数量
//        int up=0;
//        String[] title = {"业务单号","收件人姓名","收件人手机","收件人地址","品名","数量","备注","订单号"};
//        String fileName = "物流导单"+System.currentTimeMillis()+".xls";
//        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
//        String busidstr = format2.format(new Date());
//        String sheetName = "HSYS"+busidstr;
//        OrderExport errorOO=new OrderExport();
//        Set<String> listSupplier=new HashSet<>();
//        String supName="";
//        String suname="贵州荟生生物科技有限公司";
//        try {
//            for (int i = 0; i < list.size(); i++) {
//                OrderExport orderExport = list.get(i);
//                String supplier = orderExport.getSupplier() == null ? "" : orderExport.getSupplier();
//                if ("".equals(supplier)) {
//                    supName = "未知";
//                } else {
//                    supName = supplier;
//                }
//                if(!supName.equals(suname)){
//                    listSupplier.add(supName);
//                }
//                errorOO = list.get(i);
//                String[] strings = orderExport.getOrderNo().split(",");
//                if (strings.length == 1) {
//                    hashSet.add(orderExport.getOrderNo());
//                } else {
//                    for (String str : strings) {
//                        hashSet.add(str);
//                    }
//                }
//                //获取所有用户收货信息
//                RemoveUserAndAddress orderExport1 = new RemoveUserAndAddress();
////                orderExport1.setUserId(orderExport.getUserId());
//                orderExport1.setLogname(orderExport.getLogname());
//                orderExport1.setPhone(orderExport.getPhone());
//                orderExport1.setAddressAll(orderExport.getAddressAll());
//                String name = list.get(i).getGuigeName1();
//                //orderExport1.setAddressId(orderExport.getAddressId());
//                listOther.add(orderExport1);
//                //获取所有用户收货信息结束
//                int productId = 0, guigeId = 0, productCount = 0;
//                productId = list.get(i).getProductId();
//                guigeId = list.get(i).getGuigeId();
//                productCount = list.get(i).getProductCount();
//                //判断产品大于一定数量是否拆单
//                int finalProductId = productId;
//                int finalGuigeId = guigeId;
//                //判断产品大于一定数量是否拆单
//                boolean isChai = demolitionOrders.stream().anyMatch(str -> (finalProductId == str.getProductid() && finalGuigeId == str.getGuigeid() && str.getType() == 1));
//                boolean isChai1 = demolitionOrders.stream().anyMatch(str -> (finalProductId == str.getProductid() && finalGuigeId == str.getGuigeid() && str.getType() == 2));
//                if (!suname.equals(supName)) {
//                    listTempSupplier.add(orderExport);
//                } else if (isChai) {//类型为1拆单
//                    for (int n = 0; n < productCount; n++) {
//                        OrderExport orderExportTemp = orderExport;
//                        orderExportTemp.setGuigeName(name + "*" + 1);
//                        listTemp.add(orderExportTemp);
//                        number++;
//                    }
//                } else if (isChai1) {//类型为2拆单
//                    int c = productCount / 6;// a-(a/b)*b
//                    if (c == 1) {//单瓶整除为1
//                        OrderExport orderExportTemp = exportService.orderGroup(orderExport);
//                        orderExportTemp.setGuigeName(name + "*" + 6);
//                        listTemp.add(orderExportTemp);
//                        number++;
//                    }
//                    if (c > 1) {//单瓶整除大于1
//                        for (int n = 0; n < c; n++) {
//                            OrderExport orderExportTemp = exportService.orderGroup(orderExport);
//                            orderExportTemp.setGuigeName(name + "*" + 6);
//                            listTemp.add(orderExportTemp);
//                            number++;
//                        }
//                    }
//                    if (productCount % 6 != 0) {//处理剩下不到一件的
//                        int d = productCount - (productCount / 6) * 6;
//                        OrderExport orderExportTemp = exportService.orderGroup(orderExport);
//                        orderExportTemp.setGuigeName(name + "*" + d);
//                        listTemp.add(orderExportTemp);
//                        number++;
//                    }
//                } else {
//                    //添加到特殊产品拆单以外的其它集合
//                    listTempOther.add(orderExport);
//                }
//            }
//            //根据用户合并其它产品
//            //去除用户重复的收货信息
//            List<RemoveUserAndAddress> list1 = exportService.removeDuplicate(listOther);
//
//            //根据去重的用户收货信息进行整合特殊以外产品信息
//            List<OrderExport> listTempXX = new ArrayList<>();
//            for (String str : listSupplier) {
//                for (RemoveUserAndAddress item : list1) {
//                    List<OrderExport> list2 = listTempSupplier.stream().filter(c -> (
//                            c.getLogname() != null && c.getLogname().equals(item.getLogname()) &&
//                                    c.getPhone() != null && c.getPhone().equals(item.getPhone()) &&
//                                    c.getAddressAll() != null && c.getAddressAll().equals(item.getAddressAll()))
//                    ).collect(Collectors.toList());
//                    Collections.sort(list2, Comparator.comparingInt(OrderExport::getProductCount));//根据数量排序
//                    if (list2 != null && list2.size() > 0) {
//                        listTempXX = exportService.assOrderDataName(list2,str);
//                    }
//                    if (list2 != null && list2.size() > 0) {
//                        if(listTempXX!=null&&listTempXX.size()>0){
//                            List<OrderExport> list1q=listTempXX.stream().filter(c -> (!c.getOrderNo().equals("null")&&!c.getOrderNo().equals("")))
//                                    .collect(Collectors.toList());
//                            listTempXX=list1q;
//                            listTemp.addAll(listTempXX);
//                            number = number + listTempXX.size();
//                        }
//                    }
//                }
//            }
//            for (RemoveUserAndAddress item : list1) {
//                List<OrderExport> list2 = listTempOther.stream().filter(c -> (
////                                    c.getUserId()!=null&&c.getUserId().equals(item.getUserId())&&
//                                c.getLogname() != null && c.getLogname().equals(item.getLogname()) &&
//                                        c.getPhone() != null && c.getPhone().equals(item.getPhone()) &&
//                                        c.getAddressAll() != null && c.getAddressAll().equals(item.getAddressAll()))
//                ).collect(Collectors.toList());
//                Collections.sort(list2, Comparator.comparingInt(OrderExport::getProductCount));//根据数量排序
//                List<OrderExport> listTemp1 = new ArrayList<>();
//                if (list2 != null && list2.size() > 0) {
//                    listTemp1 = exportService.assOrderData(list2);
//                }
//
//                //再次进行组合
//                if (list2 != null && list2.size() > 0) {
//                    if(listTemp1!=null&&listTemp1.size()>0){
//                        listTemp.addAll(listTemp1);
//                        number = number + listTemp1.size();
//                    }
//                }
//            }
//
//        }catch(Exception e){
//            up = 1;
//        }
//        //组合数据添加到Excel
//        if(listTemp!=null&&listTemp.size()>0){
//            Collections.sort(listTemp, Comparator.comparing(OrderExport::getLogname));//排序
//        }
//
//        String [][] content=new String[listTemp.size()][];
////
////        if(listTemp.size()<=0){
////            content=new String[1][];
////            content[0] = new String[title.length];
////            if(error.equals("")){
////                content[0][0] = "空空如也!";content[0][1] = "空空如也!";content[0][2] = "空空如也!";content[0][3] = "空空如也!";
////                content[0][4] = "空空如也!";content[0][5] = "空空如也!";content[0][6] = "空空如也!";
////            }else {
////                content[0][0] = error;
////            }
////        }
////        for (int i = 0; i < listTemp.size(); i++){
////            OrderExport orderExport=listTemp.get(i);
////            content[i] = new String[title.length];
////            content[i][0] = busidstr+"-"+(i+1);//业务单号
////            content[i][1] = orderExport.getLogname();
////            content[i][2] = orderExport.getPhone();
////            content[i][3] = orderExport.getAddressAll();
////            content[i][4] = orderExport.getGuigeName();
////            content[i][5] = "";
////            content[i][6] = orderExport.getSupplier();
////            content[i][7] =orderExport.getOrderNo();
////        }
//        //创建HSSFWorkbook
//        listSupplier.add(suname);
//        List<String> listSup1 = new ArrayList<>(listSupplier);
//        listSup1.remove(suname);
//        List<String> ss=new ArrayList<>();
//        ss.add(suname);
//        ss.addAll(listSup1);
//        List<File> wb = ExcelUtil.getHSSFWorkbookZip(sheetName, title, content, null,ss,listTemp);
//        String url="";
//        String zipUrl="";
//        try {
//            String path="/usr/local/mall_uploads/logistics";
//            //String path="D:/logzip";
//            File uploadPath=new File(path);
//            if(!uploadPath.exists()){
//                uploadPath.mkdirs();
//            }
//            zipUrl="HSYS"+System.currentTimeMillis()+".zip";
//            url="/logistics/"+zipUrl;
//            FileOutputStream fos2 = new FileOutputStream(uploadPath+"/"+zipUrl);
//            toZip(wb,fos2);
//
////            try {
////                setResponseHeader(response,zipUrl);
////                OutputStream os = response.getOutputStream();
////                FileInputStream fis = new FileInputStream(uploadPath+"/"+zipUrl);
////                int n = 0;
////                byte[] buff = new byte[1024];
////                while ((n = fis.read(buff)) != -1) {
////                    os.write(buff, 0, n);
////                }
////                os.flush();
////                os.close();
////            } catch (Exception e) {
////                e.printStackTrace();
////            }
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
////        //
////        String url="";
////        //保存excel
////        try {
////            String path="/usr/local/mall_uploads/logistics";
////            File uploadPath=new File(path);
////            if(!uploadPath.exists()){
////                uploadPath.mkdirs();
////            }
////            FileOutputStream fileOut = new FileOutputStream(uploadPath+"/"+fileName);
////            url="/logistics/"+fileName;
////            wb.write(fileOut);
////            fileOut.close();
////        }
////        catch (Exception e){
////            e.printStackTrace();
////            up=1;
////        }
//        //响应到客户端
////        ZipOutputStream zos = null ;
////        try {
////            setResponseHeader(response,zipUrl);
////            OutputStream os = response.getOutputStream();
////            zos.write(os);
////            os.flush();
////            os.close();
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
//
//        if(up==0){
//            //更新为导单状态
//            List lists=null;
//            try {
//                Object[] list1X=hashSet.toArray();
//                String[] stringArray = Arrays.copyOf(list1X, list1X.length, String[].class);
//                lists = java.util.Arrays.asList(stringArray);
//            }catch (Exception e){}
//
//            if(lists!=null&&lists.size()>0){
//                String orderNos=hashSet.toString().trim().replace('[','(').replace(']',')');
//                int res=orderService.upOrderFrom(lists,new Date());
//                ExportOrder exportOrder=new ExportOrder();
//                exportOrder.setNumber(number);
//                exportOrder.setType(1);
//                exportOrder.setTypename("快递");
//                exportOrder.setUrl(url);
//                exportOrder.setTime(new Date());
//                exportOrder.setOrdernos(orderNos);
//                exportOrder.setAdminuserid(10);
//                int res1=exportOrderService.insertSelective(exportOrder);
//            }
//        }
//    }
}
